//
//  MainViewController.swift
//  新浪微博
//
//  Created by yzl on 15/7/27.
//  Copyright © 2015年 yjc. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
     addChildViewControllers()
        print(tabBar.subviews)
    }
    // 重写一个已经加载的方法
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        setupComposeBtn()
        
    }

    // 设置按钮的位置
  private func setupComposeBtn(){
    let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
    let h = tabBar.bounds.height
    let rect = CGRectMake(0, 0, w, h )
    composeButton.frame = CGRectOffset(rect, 2*w, 0)
    
}
       private func addChildViewControllers() {
       addChildViewController(HomeTableViewController(), title: "首页", Imagename: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "讯息", Imagename: "tabbar_message_center")
        /// 创建一个系统的控制器
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", Imagename: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", Imagename: "tabbar_profile")
        
    }
    
    private func addChildViewController(vc:UIViewController,title:String,Imagename:String) {
        //let vc = HomeTableViewController()
        tabBar.tintColor = UIColor.orangeColor()
        vc.title = title
        let nav = UINavigationController(rootViewController: vc)
      
      vc.tabBarItem.image = UIImage(named:Imagename)
        addChildViewController(nav)
    }
    // 监听按钮点击
    func composeButtonClick(){
       // print(composeButton)
    }

/// 懒加载按钮
    lazy private var composeButton:UIButton = {
        let btn = UIButton()
        self.tabBar.addSubview(btn)
        // 设置按钮的样式
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
          btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
}

